﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstrumentCheck.Module;

namespace InstrumentCheck.WebUI
{
    public partial class uploadAttach : BasePage
    {
        public int FlowNO { set; get; }
        public List<Attach> listAttach { set; get; }
        //public FormStatus status { set; get; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["id"] != null)
                {
                    FlowNO = 20;
                    listAttach = new List<Attach>();
                    listAttach = InstrumentCheck.BLL.CorrectQuery_bll.getAttach(Request["id"]);
                    BindGridViewAttach(listAttach);

                }
                switch (FlowNO)
                {
                    case 0:
                    case 30:
                        pnlUpload.Visible = true;
                        break;
                    default:
                        pnlUpload.Visible = false;
                        break;
                }
                if (listAttach == null)
                    listAttach = new List<Attach>();

                ViewState["list"] = listAttach;
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!upload.HasFile)
                return;

            string strFileOriginalName = upload.FileName;  //原始文件名
            string strNameAll = upload.PostedFile.FileName;  //带路径的原始文件名
            string type = strFileOriginalName.Substring(strFileOriginalName.LastIndexOf('.') + 1);

            try
            {
                string strServerName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
                upload.SaveAs(PublicFunction.strAttachPath + "\\" + strServerName);

                Attach attach = new Attach();
                attach.ClientName = strFileOriginalName;
                attach.SystemName = strServerName;
                attach.Type = AttachmentType.Attachment;
                List<Attach> list = ViewState["list"] as List<Attach>;
                list.Add(attach);
                BindGridViewAttach(list);
            }
            catch
            {
                throw;
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            //需要把上传的信息返回到父页面，并关闭当前页面
            string strAttachURL = "",strAttachInfo = "";
            List<Attach> list = ViewState["list"] as List<Attach>;
            for (int i = 0; i < list.Count; i++)
            {
                strAttachURL += "<a target=\"_blank\" href=\"" + AttachURL() + list[i].SystemName + "\">" + list[i].ClientName + "</a>;";
                strAttachInfo += list[i].SystemName + "~";
                strAttachInfo += list[i].ClientName + ";";
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Attach", "Attach('" + strAttachURL + "','" + strAttachInfo + "')",true);
        }

        private void BindGridViewAttach(List<Attach> listAttach)
        {
            gvAttach.DataSource = listAttach;
            gvAttach.DataBind();

            if (listAttach == null || listAttach.Count == 0)
                gvAttach.Visible = false;
            else
                gvAttach.Visible = true;
        }

        protected void gvAttach_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Visible = false;

                if (this.FlowNO == 0 || this.FlowNO == 30)
                    e.Row.Cells[3].Visible = true;
                else
                    e.Row.Cells[3].Visible = false;
            }

            //构造链接
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Text = "<a target='_blank' href='" + AttachURL() + e.Row.Cells[1].Text + "'>" + e.Row.Cells[0].Text + "</a>";
            }
        }

        protected void gvAttach_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<Attach> list = ViewState["list"] as List<Attach>;
            string strSystemName = gvAttach.Rows[e.RowIndex].Cells[1].Text.Trim();

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].SystemName == strSystemName)
                {
                    list.Remove(list[i]);
                    break;
                }
            }
            BindGridViewAttach(list);
            ViewState["list"] = list;
        }

    }
}
